library(tidyverse)
library(boot)

# ggplot theme
theme_pub <- function(..., rm.text = FALSE) {
  theme_bw(...) +
    theme(
      text = element_text(size=14),
      strip.background = element_blank(),
      panel.grid.minor = element_line(color='grey92', size=.1),
      panel.border = element_blank(),
      panel.spacing = unit(.02, 'npc'),
      axis.line.x = element_line(color='black', size=1),
      axis.title.x = element_text(margin=margin(10, 0, 0, 0)),
      axis.line.y = element_line(color='black', size=1),
      axis.title.y = element_text(margin=margin(0, 10, 0, 0)),
      axis.text = element_text(size=unit(13, 'points')),
      legend.position = 'bottom',
      legend.background = element_blank(),
      legend.title = element_blank(),
      legend.text = element_text(size=unit(14, 'points')),
      legend.key = element_rect(colour=NA),
      legend.key.width = unit(2.5, 'cm'),
      legend.key.size = unit(1.5, 'lines'),
      strip.text = element_text(size=14)
    )
}

# function to bootstrap-calc 95% CI and return as data frame (ready for dplyr)
fCalcBootstrapCI <- function(.tData) {
  b <- boot(.tData, function(.tDataSub, .i) sum(.tDataSub$boxes_include_helpline[.i])/sum(.tDataSub$results_reached[.i]), 1000)
  ci <- boot.ci(b, type='perc')
  return(data.frame(
    n = nrow(.tData),
    helpline = 100*sum(.tData$boxes_include_helpline)/sum(.tData$results_reached),
    ci_l = 100*ci$percent[4],
    ci_u = 100*ci$percent[5]
  ))
}



runs <- readRDS('runs.rds')

nrow(runs)

runs %>% 
  group_by(country) %>% 
  summarise(
    n = n(),
    mean_runtime = mean(runtime),
    sd_runtime = sd(runtime)
  ) %>% 
  mutate(
    share = 100*n/nrow(runs)
  )

runs %>% 
  summarise(
    n = n(),
    mean_runtime = mean(runtime),
    sd_runtime = sd(runtime)
  ) %>% 
  mutate(
    share = 100*n/nrow(runs)
  )

runs %>%
  mutate(
    country = factor(country, 
                     levels=c('at', 'be', 'de', 'ch'),
                     labels=c('Austria', 'Belgium', 'Germany', 'Switzerland')),
    agent = factor(agent,
                   levels=c('control', 'helpful', 'harmful', 'celebrity', 'celebrity_cue'),
                   labels=c(' Suicide-Unrelated Control Terms',
                            ' General Suicide-Related Helpful Terms',
                            ' General Suicide-Related Harmful Terms',
                            ' Celebrities who Died by Suicide',
                            ' Celebrities who Died by Suicide with Suicide Reference'))
  ) %>% 
  group_by(country, run_date_weekday, agent) %>%
  do(fCalcBootstrapCI(.)) %>%
  ggplot(aes(run_date_weekday, helpline, group=agent, linetype=agent, shape=agent, color=agent)) +
  geom_ribbon(aes(ymin=ci_l, ymax=ci_u), fill='grey90', color=NA) +
  geom_line(size=.7) +
  geom_point(size=3.3) +
  facet_grid(country ~ .) +
  scale_x_discrete(name='') +
  scale_y_continuous(limits=c(0, 40), breaks=seq(0, 40, 10), name='Share of SPR shown [%]\n ') +
  theme_bw() +
  theme(legend.position='bottom') +
  scale_colour_brewer(palette='Paired') +
  theme_pub() +
  guides(group=guide_legend(ncol=1), linetype=guide_legend(ncol=1), shape=guide_legend(ncol=1), color=guide_legend(ncol=1))
ggsave('figure1.png', device='png', width=14, height=11)


fCalcBootstrapCI(runs %>% filter(country == 'de' | country == 'ch') %>% filter(agent == 'harmful'))
fCalcBootstrapCI(runs %>% filter(country == 'de' | country == 'ch') %>% filter(agent == 'helpful'))

fCalcBootstrapCI(runs %>% filter(country == 'de') %>% filter(agent == 'harmful'))
fCalcBootstrapCI(runs %>% filter(country == 'de') %>% filter(agent == 'helpful'))

fCalcBootstrapCI(runs %>% filter(country == 'ch') %>% filter(agent == 'harmful'))
fCalcBootstrapCI(runs %>% filter(country == 'ch') %>% filter(agent == 'helpful'))

runs %>% 
  filter(agent == 'celebrity' | agent == 'celebrity_cue') %>% 
  nrow

runs %>% 
  filter(query == 'selbstmord') %>% 
  group_by(country) %>% 
  summarise(
    n = n(),
    shown = sum(boxes_include_helpline)
  )
